package com.cskaoyan.service;

import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public ListData<User> queryList
            (Integer page, Integer limit, String sort, String order, String username, String mobile) {
        //page页码
        //limit 当前页的大小
        PageHelper.startPage(page, limit);//修改了我们预编译的sql语句
        //查询条件
        UserExample userExample = new UserExample();
        userExample.setOrderByClause(sort + " " + order);
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }


        List<User> users = userMapper.selectByExample(userExample);
        ListData<User> listData = new ListData<User>();
        listData.setItems(users);
        //listData.setTotal(users.size());//分页后的数据量
        //PageInfo
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getTotal();//没有分页的情况下，总的数据量
        listData.setTotal((int)total);
        //给前端封装listData：items，total
        listData.setTotal((int) total);

        return listData;
    }
}
